<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class MemberAutoIdentity extends CUserIdentity {

    private $_id;

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate() {
        $network = sys_network::model()->find('network_mid=:username', array(':username' => $this->username));
        if ($network === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            //jika yang login bukan titik utama, maka menggunakan data titik utama
            if ($network->network_group_network_id != $network->network_id) {
                $network = sys_network::model()->find('network_id=:username', array(':username' => $network->network_group_network_id));
            }
            
            $this->_id = $network->network_id;
            $member = sys_member::model()->findByPk($network->network_id);

            $this->setState('network_id', $network->network_id);
            $this->setState('network_mid', $network->network_mid);
            $this->setState('network_sponsor_network_id', $network->network_sponsor_network_id);
            $this->setState('network_upline_network_id', $network->network_upline_network_id);
            $this->setState('member_name', $member->member_name);
            $this->setState('member_nickname', $member->member_nickname);
            $this->setState('member_join_datetime', $member->member_join_datetime);
            $this->setState('member_serial_id', $member->member_serial_id);
            $this->setState('member_image', ($member->member_image) ? $member->member_image : '_default.jpg');
            $this->setState('member_last_login', $member->member_last_login);
            
            $this->errorCode = self::ERROR_NONE;
        }
        return!$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }

}